package com.unitd.frame.springweb.upload.multipart.part;

import com.unitd.frame.springweb.upload.multipart.stream.PartInputStream;

import javax.servlet.ServletInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @desc 上传文件分片的参数信息类
 * @filename ParamPart.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2017/4/10
 */
public class ParamPart extends Part {

	private byte[] value;
	private String encoding;

	public ParamPart(String name, ServletInputStream in, String boundary, String encoding) throws IOException {
		super(name);
		this.encoding = encoding;
		PartInputStream pis = new PartInputStream(in, boundary);
		ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
		byte[] buf = new byte[128];

		int read;
		while ((read = pis.read(buf)) != -1) {
			baos.write(buf, 0, read);
		}

		pis.close();
		baos.close();
		this.value = baos.toByteArray();
	}

	public byte[] getValue() {
		return this.value;
	}

	public String getStringValue() throws UnsupportedEncodingException {
		return this.getStringValue(this.encoding);
	}

	public String getStringValue(String encoding) throws UnsupportedEncodingException {
		return new String(this.value, encoding);
	}

	@Override
	public boolean isParam() {
		return true;
	}
}